package com.horizon.mobile.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.horizon.mobile.R;
import com.horizon.mobile.manage.SharePreferences;
import com.horizon.mobile.model.Result;
import com.horizon.mobile.model.User;
import com.horizon.mobile.util.HttpRequest;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.interfaces.OnBottomDragListener;
import zuo.biao.library.manager.HttpManager.OnHttpResponseListener;
import zuo.biao.library.util.JSON;

public class LoginActivity extends BaseActivity implements OnBottomDragListener {
    ImageView configIV;
    EditText userEt;
    EditText pwdEt;
    Button loginBt;
    CheckBox savepassword_checkbox;
    SharePreferences sPreferences;
    RadioGroup radioGroup;
    RadioButton nwRb, wwRb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login, this);

        intent = getIntent();
        sPreferences = SharePreferences
                .getInstance(context);
        // 功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        // 功能归类分区方法，必须调用>>>>>>>>>>
    }

    @Override
    public Activity getActivity() {
        // TODO Auto-generated method stub
        return this;
    }

    @Override
    public void initView() {
        userEt = (EditText) findViewById(R.id.user_name);
        pwdEt = (EditText) findViewById(R.id.password);
        radioGroup = (RadioGroup) findViewById(R.id.neiwangRG);
        loginBt = (Button) findViewById(R.id.sign_in_button);
        configIV = (ImageView) findViewById(R.id.configIV);
        savepassword_checkbox = (CheckBox) findViewById(R.id.savepassword_checkbox);
        nwRb = (RadioButton) findViewById(R.id.nwRB);
        wwRb = (RadioButton) findViewById(R.id.wwRB);
    }

    @Override
    public void initData() {
        // TODO Auto-generated method stub
        userEt.setText(sPreferences.getLoginName());
        pwdEt.setText(sPreferences.getPassword());
        if(sPreferences.getIsNeiwang()){
            nwRb.setChecked(true);
        }else{
            wwRb.setChecked(true);
        }
    }

    @Override
    public void initEvent() {


        configIV.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(LoginActivity.this,
                        ConfigActivity.class);
                toActivity(intent);
            }
        });
        loginBt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (nwRb.isChecked()) {
                    sPreferences.putIsNeiwang(true);
                } else {
                    sPreferences.putIsNeiwang(false);
                }
                if (userEt.getText().toString().length() == 0) {
                    showShortToast("用户名不能为空！");
                    return;
                }
                if (pwdEt.getText().toString().length() == 0) {
                    showShortToast("密码不能为空！");
                    return;
                }
                showProgressDialog("正在登录，请稍后...");
                HttpRequest.login(userEt.getText().toString(), pwdEt.getText()
                        .toString(), 0, loginListener);
            }
        });
    }

    @Override
    public void onDragBottom(boolean rightToLeft) {
        // TODO Auto-generated method stub
        finish();
    }

    OnHttpResponseListener loginListener = new OnHttpResponseListener() {

        @Override
        public void onHttpResponse(int requestCode, String resultJson,
                                   Exception e) {
            System.out.println("resultJson--------------->"+resultJson);
            // TODO Auto-generated method stub
            boolean isNull = resultJson == null;
            dismissProgressDialog();
            if (resultJson != null) {
                Result result = JSON.parseObject(resultJson, Result.class);
                if (result != null && result.isSuccess()) {
                    User user = JSON.parseObject(result.getData().toString(),
                            User.class);
                    if (savepassword_checkbox.isChecked()) {
                        sPreferences.putIsLogin(true);
                        sPreferences.putPassword(pwdEt.getText().toString());
                        sPreferences.putLoginName(userEt.getText().toString());
                        sPreferences.putTenantCode(user.getTenantCode());
                    }
                    sPreferences.putRealName(user.getUserName());
                    sPreferences.putDeptName(user.getDeptName());
                    sPreferences.putToken(user.getAccessToken());
                    Intent intent = new Intent(LoginActivity.this,
                            MainActivity.class);
                    toActivity(intent);
                    finish();
                } else {
                    if (result != null)
                        showShortToast(result.getMessage());
                    else
                        showShortToast("连接服务器失败！");
                }
            } else {
                if (e != null)
                    showShortToast("连接服务器失败，请检查配置地址！");
                else
                    showShortToast("连接服务器失败，请检查配置地址！");
            }

        }
    };

}
